package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.link.view.adapter.PickAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;

import java.util.List;

public class TeamMultiPopWindow extends BasePopWindow {

    public static final int TYPE_ONE = 1;
    public static final int TYPE_TWO = 2;
    public static final int TYPE_THR = 3;
    public static final int TYPE_FOU = 4;
    public static final int TYPE_FIV = 5;
    public static final int TYPE_SIX = 6;
    public static final int TYPE_OTHER = -2;
    public static final int TYPE_DEFAULT = -1;

    private FilterResultClick filterResultClick;
    private IItemClick iItemClick;

    private NestedScrollView mScrollView;
    private LinearLayout ll2;
    private LinearLayout ll3;
    private LinearLayout ll4;
    private LinearLayout ll5;
    private LinearLayout ll6;
    private PickAdapter mAdapter1;
    private PickAdapter mAdapter2;
    private PickAdapter mAdapter3;
    private PickAdapter mAdapter4;
    private PickAdapter mAdapter5;
    private PickAdapter mAdapter6;
    private TextView mTvName1;
    private int position1 = -1;
    private int position2 = -1;
    private int position3 = -1;
    private int position4 = -1;
    private int position5 = -1;
    private int position6 = -1;
    private int mType = TYPE_DEFAULT;
    private int mMaxNumber;

    public interface FilterResultClick {
        void result(PickSelectEntity... entity);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public TeamMultiPopWindow(Context context) {
        super(context, DIR_RIGHT);
    }

    public TeamMultiPopWindow(Context context,int maxNumber) {
        super(context, DIR_RIGHT);
        this.mMaxNumber = maxNumber;
    }

    @Override
    protected int popLayout() {
        return R.layout.team_multi_popwindow;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        setAnimationStyle(com.swgk.core.R.style.popwindow_down_up);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        mScrollView = holder.getPop().findViewById(R.id.nestedScrollView);
        TextView okView = holder.getPop().findViewById(R.id.ok_view);
        TextView reSetView = holder.getPop().findViewById(R.id.reset_view);
        MaxRecyclerView rv1 = holder.getPop().findViewById(R.id.rv_pro_type);
        MaxRecyclerView rv2 = holder.getPop().findViewById(R.id.rv_zy_type);
        MaxRecyclerView rv3 = holder.getPop().findViewById(R.id.rv_yw_type);
        MaxRecyclerView rv4 = holder.getPop().findViewById(R.id.rv_four_type);
        MaxRecyclerView rv5 = holder.getPop().findViewById(R.id.rv_five_type);
        MaxRecyclerView rv6 = holder.getPop().findViewById(R.id.rv_six_type);
        mTvName1 = holder.getPop().findViewById(R.id.tv_name1);

        ll2 = holder.getPop().findViewById(R.id.ll_zy);
        ll3 = holder.getPop().findViewById(R.id.ll_yw);
        ll4 = holder.getPop().findViewById(R.id.ll_four);
        ll5 = holder.getPop().findViewById(R.id.ll_five);
        ll6 = holder.getPop().findViewById(R.id.ll_six);

        ll2.setVisibility(View.GONE);
        ll3.setVisibility(View.GONE);
        ll4.setVisibility(View.GONE);
        ll5.setVisibility(View.GONE);
        ll6.setVisibility(View.GONE);

        okView.setOnClickListener(v -> {
            if (mMaxNumber != 0){
                boolean flag = (mMaxNumber == 1 && position1 < 0) || (mMaxNumber == 2 && position2 < 0) || (mMaxNumber == 3 && position3 < 0) || (mMaxNumber == 4 && position4 < 0) || (mMaxNumber == 5 && position5 < 0) || (mMaxNumber == 6 && position6 < 0);
                if (flag){
                    MToast.showToast(mContext,"必须选择前3级");
                    return;
                }
            }

            if (filterResultClick != null) {
                if (position1 >= 0 && position2 >= 0 && position3 >= 0 && position4 >= 0 && position5 >= 0 && position6 >= 0) {
                    filterResultClick.result(
                            mAdapter1.getDatas().get(position1),
                            mAdapter2.getDatas().get(position2),
                            mAdapter3.getDatas().get(position3),
                            mAdapter4.getDatas().get(position4),
                            mAdapter5.getDatas().get(position5),
                            mAdapter6.getDatas().get(position6));
                } else if (position1 >= 0 && position2 >= 0 && position3 >= 0 && position4 >= 0 && position5 >= 0) {
                    filterResultClick.result(
                            mAdapter1.getDatas().get(position1),
                            mAdapter2.getDatas().get(position2),
                            mAdapter3.getDatas().get(position3),
                            mAdapter4.getDatas().get(position4),
                            mAdapter5.getDatas().get(position5));
                } else if (position1 >= 0 && position2 >= 0 && position3 >= 0 && position4 >= 0) {
                    filterResultClick.result(
                            mAdapter1.getDatas().get(position1),
                            mAdapter2.getDatas().get(position2),
                            mAdapter3.getDatas().get(position3),
                            mAdapter4.getDatas().get(position4));
                } else if (position1 >= 0 && position2 >= 0 && position3 >= 0) {
                    filterResultClick.result(
                            mAdapter1.getDatas().get(position1),
                            mAdapter2.getDatas().get(position2),
                            mAdapter3.getDatas().get(position3));
                } else if (position1 >= 0 && position2 >= 0) {
                    filterResultClick.result(
                            mAdapter1.getDatas().get(position1),
                            mAdapter2.getDatas().get(position2));
                } else if (position1 >= 0) {
                    filterResultClick.result(
                            mAdapter1.getDatas().get(position1));
                } else {
                    filterResultClick.result();
                }
            }
            dissMiss();
        });

        reSetView.setOnClickListener(view -> {
            mAdapter1.resetCheckDatas();
            mAdapter2.resetCheckDatas();
            mAdapter3.resetCheckDatas();
            mAdapter4.resetCheckDatas();
            mAdapter5.resetCheckDatas();
            mAdapter6.resetCheckDatas();
            mAdapter1.setDefItem(-1);
            mAdapter2.setDefItem(-1);
            mAdapter3.setDefItem(-1);
            mAdapter4.setDefItem(-1);
            mAdapter5.setDefItem(-1);
            mAdapter6.setDefItem(-1);
            position1 = -1;
            position2 = -1;
            position3 = -1;
            position4 = -1;
            position5 = -1;
            position6 = -1;
            ll2.setVisibility(View.GONE);
            ll3.setVisibility(View.GONE);
            ll4.setVisibility(View.GONE);
            ll5.setVisibility(View.GONE);
            ll6.setVisibility(View.GONE);
            if (filterResultClick != null) {
                filterResultClick.result();
            }
            dissMiss();
        });

        mAdapter1 = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        mAdapter1.setItemSelect(true);
        mAdapter1.setDefItem(-1);
        rv1.setLayoutManager(new GridLayoutManager(mContext, 4));
        rv1.setAdapter(mAdapter1);

        mAdapter1.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                mAdapter2.setDefItem(-1);
                mAdapter3.setDefItem(-1);
                mAdapter4.setDefItem(-1);
                mAdapter5.setDefItem(-1);
                mAdapter6.setDefItem(-1);
                ll2.setVisibility(View.GONE);
                ll3.setVisibility(View.GONE);
                ll4.setVisibility(View.GONE);
                ll5.setVisibility(View.GONE);
                ll6.setVisibility(View.GONE);
                mType = TYPE_TWO;
                position1 = position;
                position2 = -1;
                position3 = -1;
                position4 = -1;
                position5 = -1;
                position6 = -1;
                if (iItemClick != null) {
                    iItemClick.itemClick(pickSelectEntity.getNo(), position, pickSelectEntity.getProjectscale());
                }
            }
        });

        mAdapter2 = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        mAdapter2.setItemSelect(true);
        mAdapter2.setDefItem(-1);
        rv2.setLayoutManager(new GridLayoutManager(mContext, 4));
        rv2.setAdapter(mAdapter2);

        mAdapter2.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                mAdapter3.setDefItem(-1);
                mAdapter4.setDefItem(-1);
                mAdapter5.setDefItem(-1);
                mAdapter6.setDefItem(-1);
                ll3.setVisibility(View.GONE);
                ll4.setVisibility(View.GONE);
                ll5.setVisibility(View.GONE);
                ll6.setVisibility(View.GONE);
                mType = TYPE_THR;
                position2 = position;
                position3 = -1;
                position4 = -1;
                position5 = -1;
                position6 = -1;
                if (iItemClick != null) {
                    iItemClick.itemClick(pickSelectEntity.getNo(), position, pickSelectEntity.getProjectscale());
                }
            }
        });

        mAdapter3 = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        mAdapter3.setItemSelect(true);
        mAdapter3.setDefItem(-1);
        rv3.setLayoutManager(new GridLayoutManager(mContext, 4));
        rv3.setAdapter(mAdapter3);

        mAdapter3.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                mAdapter4.setDefItem(-1);
                mAdapter5.setDefItem(-1);
                mAdapter6.setDefItem(-1);
                ll4.setVisibility(View.GONE);
                ll5.setVisibility(View.GONE);
                ll6.setVisibility(View.GONE);
                mType = TYPE_FOU;
                position3 = position;
                position4 = -1;
                position5 = -1;
                position6 = -1;
                if (iItemClick != null) {
                    iItemClick.itemClick(pickSelectEntity.getNo(), position, pickSelectEntity.getProjectscale());
                }
            }
        });
        mAdapter4 = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        mAdapter4.setItemSelect(true);
        mAdapter4.setDefItem(-1);
        rv4.setLayoutManager(new GridLayoutManager(mContext, 4));
        rv4.setAdapter(mAdapter4);

        mAdapter4.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                mAdapter5.setDefItem(-1);
                mAdapter6.setDefItem(-1);
                ll5.setVisibility(View.GONE);
                ll6.setVisibility(View.GONE);
                mType = TYPE_FIV;
                position4 = position;
                position5 = -1;
                position6 = -1;
                if (iItemClick != null) {
                    iItemClick.itemClick(pickSelectEntity.getNo(), position, pickSelectEntity.getProjectscale());
                }
            }
        });
        mAdapter5 = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        mAdapter5.setItemSelect(true);
        mAdapter5.setDefItem(-1);
        rv5.setLayoutManager(new GridLayoutManager(mContext, 4));
        rv5.setAdapter(mAdapter5);

        mAdapter5.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                mAdapter6.setDefItem(-1);
                ll6.setVisibility(View.GONE);
                mType = TYPE_SIX;
                position5 = position;
                position6 = -1;
                if (iItemClick != null) {
                    iItemClick.itemClick(pickSelectEntity.getNo(), position, pickSelectEntity.getProjectscale());
                }
            }
        });
        mAdapter6 = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        mAdapter6.setItemSelect(true);
        mAdapter6.setDefItem(-1);
        rv6.setLayoutManager(new GridLayoutManager(mContext, 4));
        rv6.setAdapter(mAdapter6);

        mAdapter6.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                mType = TYPE_OTHER;
                position6 = position;
                if (iItemClick != null) {
                    iItemClick.itemClick(pickSelectEntity.getNo(), position, pickSelectEntity.getProjectscale());
                }
            }
        });
    }

    public void setData1(List<PickSelectEntity> data) {
        mAdapter1.setDatas(data);
        mAdapter1.setDefItem(position1);
    }

    public void setData2(List<PickSelectEntity> data) {
        mAdapter2.setDatas(data);
        mAdapter2.setDefItem(position2);
        if (data == null || data.size() == 0) {
            ll2.setVisibility(View.GONE);
        } else {
            ll2.setVisibility(View.VISIBLE);
            //滚到底部
            mScrollView.fullScroll(View.FOCUS_DOWN);
        }
    }

    public void setData3(List<PickSelectEntity> data) {
        mAdapter3.setDatas(data);
        if (data == null || data.size() == 0) {
            ll3.setVisibility(View.GONE);
        } else {
            ll3.setVisibility(View.VISIBLE);
            //滚到底部
            mScrollView.fullScroll(View.FOCUS_DOWN);
        }
        mAdapter3.setDefItem(position3);
    }

    public void setData4(List<PickSelectEntity> data) {
        mAdapter4.setDatas(data);
        if (data == null || data.size() == 0) {
            ll4.setVisibility(View.GONE);
        } else {
            ll4.setVisibility(View.VISIBLE);
            //滚到底部
            mScrollView.fullScroll(View.FOCUS_DOWN);
        }
        mAdapter4.setDefItem(position4);
    }

    public void setData5(List<PickSelectEntity> data) {
        mAdapter5.setDatas(data);
        if (data == null || data.size() == 0) {
            ll5.setVisibility(View.GONE);
        } else {
            ll5.setVisibility(View.VISIBLE);
            //滚到底部
            mScrollView.fullScroll(View.FOCUS_DOWN);
        }
        mAdapter5.setDefItem(position5);
    }

    public void setData6(List<PickSelectEntity> data) {
        mAdapter6.setDatas(data);
        if (data == null || data.size() == 0) {
            ll6.setVisibility(View.GONE);
        } else {
            ll6.setVisibility(View.VISIBLE);
            //滚到底部
            mScrollView.fullScroll(View.FOCUS_DOWN);
        }
        mAdapter6.setDefItem(position6);
    }

    public int getPosition1() {
        return position1;
    }

    public int getPosition2() {
        return position2;
    }

    public int getPosition3() {
        return position3;
    }

    public int getType() {
        return mType;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public interface IItemClick {
        void itemClick(String firstNo, int position, String name);
    }

    public void onItemClick(IItemClick iItemClick) {
        this.iItemClick = iItemClick;
    }

    public void setName1(String name1){
        mTvName1.setText(name1);
    }

    public void setMaxNumber(int maxNumber){
        this.mMaxNumber = maxNumber;
    }
}
